<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * Upload image in folder and create thumbnail from
 *
   USAGE:
   <pre>
   $this->file_info = new Geocontexter_Model_ImageUpload;

   $params  = array('post_name'   => string,  // as defined in $_FILES
                    'data_folder' => string   // full path to the image directory
                    );

   $result  = $this->file_info->upload( $params );

   if($result instanceof Mozend_ModelError)
   {
       return $this->error( $result->getErrorString(), __file__, __line__ );
   }

   </pre>
 * @package GeoContexter
 * @subpackage Module_Geocontexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 768 $ / $LastChangedDate: 2010-12-16 16:11:56 +0100 (jeu., 16 déc. 2010) $ / $LastChangedBy: armand.turpel $
 */

namespace Youobserve\Model;

use Core\Model\AbstractModel;
use Core\Model\InterfaceModel;

class ImageUpload extends    AbstractModel
                  implements InterfaceModel
{
    /**
     * upload image
     */
    public function run( $params )
    {
		$this->build_thumbnail($params['path'], $params['dest'], $params['photo'], $params['mime'], $params['width'], $params['height'], $params['new_width']);
		$this->build_thumbnail($params['path'], $params['dest'] . '/thumbnail', $params['photo'], $params['mime'], $params['width'], $params['height'], $params['thumb_width']);
    }

    /**
     *
     */
    private function build_thumbnail($path, $dest, $image_name, $mime, $width, $height, $thumb_width)
    {
        $newwidth = $thumb_width;
        $newheight = (int) (($thumb_width / $width) * $height);

        if(false === ($thumb = imagecreatetruecolor($newwidth, $newheight)))
        {
            throw new \Exception('imagecreatetruecolor failed with params: ' . var_export(array($newwidth, $newheight),true));
        }

        switch(strtolower($mime))
        {
            case 'image/jpeg':
                if(false === ($source = imagecreatefromjpeg($path . '/' . $image_name)))
                {
                    throw new \Exception('imagecreatefromjpeg failed with param: ' . var_export($path . '/' . $image_name,true));
                }

                // Resize
                if(false === (imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height)))
                {
                    $_params = array($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                    throw new \Exception('imagecopyresized failed with params: ' . var_export($_params,true));
                }

                if(false === (imagejpeg($thumb, $dest . '/' . $image_name)))
                {
                    $_params = array($thumb, $dest . '/' . $image_name);
                    throw new \Exception('imagejpeg failed with params: ' . var_export($_params,true));
                }

                return;

            case 'image/gif':
                if(false === ($source = imagecreatefromgif($path . '/' . $image_name)))
                {
                    throw new \Exception('imagecreatefromgif failed with param: ' . var_export($path . '/' . $image_name,true));
                }

                // Resize
                if(false === (imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height)))
                {
                    $_params = array($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                    throw new \Exception('imagecopyresized failed with params: ' . var_export($_params,true));
                }

                if(false === (imagegif($thumb, $dest . '/' . $image_name)))
                {
                    $_params = array($thumb, $dest . '/' . $image_name);
                    throw new \Exception('imagegif failed with params: ' . var_export($_params,true));
                }

                return;

            case 'image/png':
                if(false === ($source = imagecreatefrompng($path . '/' . $image_name)))
                {
                    throw new \Exception('imagecreatefrompng failed with param: ' . var_export($path . '/' . $image_name,true));
                }

                // Resize
                if(false === (imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height)))
                {
                    $_params = array($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                    throw new \Exception('imagecopyresized failed with params: ' . var_export($_params,true));
                }

                if(false === (imagepng($thumb, $dest . '/' . $image_name)))
                {
                    $_params = array($thumb, $dest . '/' . $image_name);
                    throw new \Exception('imagepng failed with params: ' . var_export($_params,true));
                }

                return;
            default:
                    throw new \Exception('unknown image type: ' . var_export($this->file_info,true));
        }
    }
}